package cn.imvc.app.controllers.goods;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.entities.TypeClass;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.TypeClassModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

/**
 * 
 * @author zhanghy
 * @date 2016年8月11日上午9:44:34
 *
 */
public class TypeClassControllerImpl implements TypeClassController{
	static Logger logger = Logger.getLogger(TypeClassControllerImpl.class);
	public Page<TypeClass> page;
	public ViewRequest viewRequest;
	public TypeClass typeClass;
	public int id;
	public int[] ids;
	public String search_content;
	
	@Autowired
	TypeClassModel typeClassModel;
	@Autowired
	LoggingModel loggingModel;
	/**
	 * 多选的查找带回
	 * @return
	 */
	private String multipleDialogMyIds;
	private String multipleDialogMyNames;
	
	public String search() {
		return null;
	}

	public String index() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("sort", "ASC");
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("name_like", search_content);
		}
		page = typeClassModel.find(viewRequest);
		return "type-class/index";
	}

	public String show() {
		return null;
	}

	public String editNew() {
		return "type-class/edit";
	}

	public String create() {
		int result = typeClassModel.save(typeClass);
		if(result==0){
			return Render.json(new DialogBean("300","类型编码不能重复","type-class","closeCurrent"));
		}else if(result==-1){
			return Render.json(new DialogBean("300","类型名称不能重复","type-class","closeCurrent"));
		}
		loggingModel.info(logger,"添加类型："+typeClass.getName());
		return Render.json(new DialogBean("200","添加成功","","closeCurrent"));
	}

	public String edit() {
		typeClass = typeClassModel.get(id);
		return "type-class/edit";
	}

	public String update() {
		int result = typeClassModel.update(id,typeClass);
		if(result==0){
			return Render.json(new DialogBean("300","类型编码不能重复","type-class","closeCurrent"));
		}else if(result==-1){
			return Render.json(new DialogBean("300","类型名称不能重复","type-class","closeCurrent"));
		}
		loggingModel.info(logger,"修改类型："+typeClass.getName());
		return Render.json(new DialogBean("200","修改成功","","closeCurrent"));
	}

	public String destroy() {
		String result = typeClassModel.delete(id);
		if(result.equals("")){
			loggingModel.info(logger,"删除类型："+id);
		    return Render.json(new DialogBean("200","删除成功",""));
		}else{
			return Render.json(new DialogBean("300",result,""));
		}
	}

	public String destroyBatch() {
		typeClassModel.deleteBatch(ids);
		return Render.json(new DialogBean("200","删除成功",""));
	}
/**
 * 外部调用
 * 
 */
	public String dialog() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("sort", "ASC");
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("name_like", search_content);
		}
		page = typeClassModel.find(viewRequest);
		return "type-class/dialog";
	}
	
	/**
     * 多选的dialog
     */
    public String multipleDialog(){
    	if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("sort", "ASC");
		if(search_content != null && !search_content.trim().equals("")){
			viewRequest.getParams().put("code_like", search_content);
			viewRequest.getParams().put("_or_name_like", search_content);
			viewRequest.getParams().put("_or_sort_like", search_content);
		}
    	page = typeClassModel.find(viewRequest);
    	return "type-class/multipleDialog";
    }
    /**
     * 首页调用
     * @return
     */
	public String dialog2() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("sort", "ASC");
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("name_like", search_content);
		}
		page = typeClassModel.find(viewRequest);
		return "type-class/dialog2";
	}
	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public void setTypeClass(TypeClass typeClass) {
		this.typeClass = typeClass;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public void setSearch_content(String search_content) {
		this.search_content = search_content;
	}

	public String getMultipleDialogMyIds() {
		return multipleDialogMyIds;
	}

	public void setMultipleDialogMyIds(String multipleDialogMyIds) {
		this.multipleDialogMyIds = multipleDialogMyIds;
	}

	public String getMultipleDialogMyNames() {
		return multipleDialogMyNames;
	}

	public void setMultipleDialogMyNames(String multipleDialogMyNames) {
		this.multipleDialogMyNames = multipleDialogMyNames;
	}
	
	

}
